package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Project;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class PDao {

	DBHelp<Project> db=new DBHelp<Project>();
	
	public void save(Project project){
		String sql="insert into t_project(id,pname,pdescripe,pcreatetime) values(?,?,?,?)";
		db.executeSQL(sql,project.getId(),project.getPname(),project.getPdescripe(),project.getPcreatetime());
	}
	
	public void update(Project project){
		String sql="update t_project set pname=?,pdescripe=? where id=?";
		db.executeSQL(sql,project.getPname(),project.getPdescripe(),project.getId());
	}
	
	public List<Project> findAll(){
		String sql="select id,pname,pdescripe,pcreatetime from t_project";
		return db.queryForList(sql,new PRowMapper());
	}
	
	public Project findById(String id){
		String sql="select id,pname,pdescripe,pcreatetime from t_project where id=?";
		return db.queryForObject(sql,new PRowMapper(),id);
	}
	
	private class PRowMapper implements RowMapper<Project>{
		
		public Project mapperRow(ResultSet rs) throws SQLException{
			Project project=new Project();
			project.setId(rs.getString("id"));
			project.setPcreatetime(rs.getString("pcreatetime"));
			project.setPdescripe(rs.getString("pdescripe"));
			project.setPname(rs.getString("pname"));
			return project;
			
		}
	}
}
